package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.enums.DisableStatus;
import com.rustic.server.query.AttractionsAdminListQuery;
import com.rustic.server.dto.AttractionsAddDTO;
import com.rustic.server.dto.AttractionsUpdateDTO;
import com.rustic.server.po.Attractions;
import com.rustic.server.service.AttractionsService;
import com.rustic.server.vo.list.AttractionsAdminListVO;
import com.rustic.server.vo.AttractionsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[attractions]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/attractions")
@Api(tags = {"景点管理接口"})
public class AttractionsAdminController {

    @Autowired
    private AttractionsService attractionsService;

    @ApiOperation(value = "景点-列表", notes = "权限key: attractions-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('attractions-list')")
    public Result<Page<AttractionsAdminListVO>> list(@Validated AttractionsAdminListQuery query) {
        LambdaQueryWrapper<Attractions> queryWrapper = attractionsService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<Attractions> pageData = attractionsService.page(query.toPage(), queryWrapper);
        List<Attractions> attractionsList = pageData.getRecords();
        if (CollUtil.isEmpty(attractionsList)) {
            return Result.data(new Page<>());
        }
        List<AttractionsAdminListVO> results = attractionsService.toAdminListVo(attractionsList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "景点-详情", notes = "权限key: attractions-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "景点id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('attractions-detail')")
    public Result<AttractionsVO> detail(int id) {
        if (id <= 0) {
            return Result.error(AttractionsService.ATTRACTIONS_NOT_EXIST);
        }
        AttractionsVO attractionsVO = attractionsService.getDetailById(id);
        return Result.data(attractionsVO);
    }

    @ApiOperation(value = "景点-增加", notes = "权限key: attractions-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('attractions-add')")
    @OperateLog(description = "增加景点")
    public Result<String> add(@RequestBody @Validated AttractionsAddDTO attractionsAddDTO) {
        Attractions attractions = attractionsAddDTO.toAttractions();
        attractionsService.add(attractions);
        return Result.ok("添加景点成功");
    }

    @ApiOperation(value = "景点-更新", notes = "权限key: attractions-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('attractions-update')")
    @OperateLog(description = "更新景点")
    public Result<String> update(@RequestBody @Validated AttractionsUpdateDTO attractionsUpdateDTO) {
        Attractions attractions = attractionsUpdateDTO.toAttractions();
        attractionsService.updateAttractions(attractions);
        return Result.ok("编辑景点成功");
    }

    @ApiOperation(value = "景点-删除", notes = "权限key: attractions-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('attractions-delete')")
    @OperateLog(description = "删除景点")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(AttractionsService.ATTRACTIONS_NOT_EXIST);
        }
        Attractions attractions = attractionsService.getById(id);
        if (attractions == null) {
            return Result.error(AttractionsService.ATTRACTIONS_NOT_EXIST);
        }
        attractionsService.removeById(id);
        return Result.ok("删除景点成功");
    }

    @ApiOperation(value = "景点-发布下架", notes = "权限key: attractions-enabled")
    @PostMapping("/enabled")
    @PreAuthorize("@validate.hasPermission('attractions-enabled')")
    @OperateLog(description = "发布/下架景点")
    public Result<String> enabled(int id) {
        if (id <= 0) {
            return Result.error(AttractionsService.ATTRACTIONS_NOT_EXIST);
        }
        int status = attractionsService.enabled(id);
        String action = "下架";
        if (DisableStatus.isNormal(status)) {
            action = "发布";
        }
        return Result.ok(action + "成功");
    }
}
